import _ from 'lodash'
// 排序根据固定列重排
export function sortFixed(arr) {
  return _.sortBy(arr, [function(item) {
      if (!item.fixed) {
        return 0
      } else if (item.fixed === 'right') {
        return 1
      } else {
        return -1
      }
  }])
}
/**
 * 深拷贝
 * @param {*} obj
 */
export function deepClone(obj) {
  return JSON.parse(JSON.stringify(obj))
}

/**
 * 利用 requestAnimationFrame 进行 throttle
 * @param {function} fn
 */
export function rAF (fn) {
  let ticking = false
  let update = function (...arg) {
      ticking = false
      fn.apply(this, arg)
  }
  return function (...arg) {
      let self = this;
      if(!ticking) {
          requestAnimationFrame(function() {
              update.apply(self, arg)
          });
      }
      ticking = true;
  }
}
// 调试代码
export function debug (option = {}) {
  const prefix = option.prefix || ''
  const disabled = !!option.disabled
  return function (...arg) {
    if (process.env.NODE_ENV === 'development' && !disabled) {
      if (prefix) {
        arg.unshift('background: #222; color: #bada55')
        arg.unshift('%c ' + prefix)
      }
      // console.log('%c Oh my heavens! ', 'background: #222; color: #bada55')
      console.log(...arg)
    }
  }
}
